package com.example.entity;



import com.example.utils.JSON;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class MyGameData{

    public Map<Integer, Chef> chefsMap = new HashMap<Integer, Chef>();
    public Map<Integer, Recipe> recipesMap = new HashMap<Integer, Recipe>();
    public Map<Integer, Equip> equipsMap = new HashMap<Integer, Equip>();

    public List<Chef> chefs = new ArrayList<Chef>();
    public List<Equip> equips = new ArrayList<Equip>();
    public ArrayList<Recipe> recipes = new ArrayList<Recipe>();


    public MyGameData() {

    }


    public static MyGameData haveAll(OfficialGameData officialGameData, JsonNode foongameData) {
        MyGameData myGameData = new MyGameData();
        JsonNode recipes = foongameData.get("recipes");
        int size = recipes.size();
        //菜谱
        for (int i = 0; i < size; i++) {
            JsonNode jsonRecipe = recipes.get(i);
            int id = jsonRecipe.get("id").asInt();

            Recipe recipe = officialGameData.RecipeHashMap.get(id);

            recipe.setPrice(recipe.getPrice() + recipe.getExPrice());

            myGameData.recipes.add(recipe);

        }

        myGameData.recipesMap = myGameData.recipes.stream().collect((Collectors.toMap(x -> x.getRecipeId(), x -> x, (oldValue, newValue) -> newValue)));


        //厨师
        JsonNode chefs = foongameData.get("chefs");
        size = chefs.size();
        for (int i = 0; i < size; i++) {
            JsonNode jsonchef = chefs.get(i);

            int id = jsonchef.get("id").asInt();
            Chef chef = officialGameData.ChefHashMap.get(id);
            myGameData.chefs.add(chef);

        }
        myGameData.chefsMap = myGameData.chefs.stream().collect((Collectors.toMap(x -> x.getChefId(), x -> x)));
        JsonNode equipmentData = foongameData.get("equips");
        List<Equip> officialequips = officialGameData.getEquips();
        //厨具
        for (Equip equip : officialequips) {
            myGameData.equips.add(equip);
        }
        myGameData.equipsMap = myGameData.equips.stream().collect((Collectors.toMap(x -> x.getEquipId(), x -> x)));
        return myGameData;
    }

    //从图鉴网导入数据
    public static MyGameData importChefsAndRecipesFromFoodGame(OfficialGameData officialGameData, JsonNode foongameData) {
        MyGameData myGameData = new MyGameData();
        JsonNode recipes = foongameData.get("recipes");

        int size = recipes.size();
        //菜谱
        for (int i = 0; i < size; i++) {
            JsonNode jsonRecipe = recipes.get(i);
            int id = jsonRecipe.get("id").asInt();
            if (jsonRecipe.get("got").asText().equals("是")) {
                Recipe recipe = officialGameData.RecipeHashMap.get(id);
                if (jsonRecipe.get("ex").asText().equals("是")) {
                    recipe.setPrice(recipe.getPrice() + recipe.getExPrice());
                }
                myGameData.recipes.add(recipe);
            }
        }

        myGameData.recipesMap = myGameData.recipes.stream().collect((Collectors.toMap(x -> x.getRecipeId(), x -> x, (oldValue, newValue) -> newValue)));


        //厨师
        JsonNode chefs = foongameData.get("chefs");
        size = chefs.size();
        for (int i = 0; i < size; i++) {
            JsonNode jsonchef = chefs.get(i);

            int id = jsonchef.get("id").asInt();
            Chef chef = officialGameData.ChefHashMap.get(id);
            if (jsonchef.get("got").asText().equals("是")) {
                if (!jsonchef.get("ult").asText().equals("是")) {
                    chef.ultimateSkill = null;
                }
                myGameData.chefs.add(chef);
            }
        }
        myGameData.chefsMap = myGameData.chefs.stream().collect((Collectors.toMap(x -> x.getChefId(), x -> x)));
        JsonNode equipmentData = foongameData.get("equips");
        List<Equip> officialequips = officialGameData.getEquips();
        //厨具
        size = equipmentData.size();
        for (Equip equip : officialequips) {
            for (int i = 0; i < size; i++) {
                String equipname = equipmentData.get(i).asText();
                if (equip.getName().equals(equipname)) {
                    myGameData.equips.add(equip);
                    break;
                }
            }
        }
        myGameData.equipsMap = myGameData.equips.stream().collect((Collectors.toMap(x -> x.getEquipId(), x -> x)));
        return myGameData;
    }

    @Override
    public String toString() {
                try {
            return JSON.getObjectMapper().writeValueAsString(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


}
